<template>
<div>
   <!--头部-->

    <div id="headCheck">
      <span style="margin-left: 10px">教师工号：</span>
      <el-input
          placeholder="请输入内容"
          v-model="inputTeacherById"
          clearable
          style="width: 150px">
      </el-input>
      <el-button type="warning" round @click="searchTeacherById" style="margin-left: 10px">搜索</el-button>

      <span style="margin-left: 10px">教师姓名：</span>
      <el-input
          placeholder="请输入内容"
          v-model="inputTeacherByName"
          clearable
          style="width: 150px">
      </el-input>
      <el-button type="warning" round @click="searchTeacherByName" style="margin-left: 10px">搜索</el-button>
      <el-button type="primary" @click="addTeacherInfo">添加教师信息</el-button>
    </div>



   <!--主体-->
  <el-main id="main">
    <template>
      <el-table
          :data="tableData"
          stripe
          border
          style="width: 100%"
      >
        <el-table-column
            fixed
            prop="uid"
            label="教师工号"
        >
        </el-table-column>
        <el-table-column
            fixed
            prop="name"
            label="教师姓名"
        >
        </el-table-column>
        <el-table-column
            fixed
            prop="title"
            label="职称"
        >
        </el-table-column>
        <el-table-column
            fixed
            prop="type"
            label="身份"
            :formatter="typeFormat"
        >
        </el-table-column>
        <el-table-column
            fixed
            prop="college_id"
            label="所属学院"
            :formatter="collegeFormat"
        >
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
        >
          <template slot-scope="scope">
            <el-button
                @click="editRow(scope.$index, scope.row)"
                type="warning"
                size="small">
              编辑
            </el-button>
            <el-button
                @click="deleteRow(scope.$index, scope.row)"
                type="danger"
                size="small">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--添加教师信息表单-->
      <el-dialog title="添加教师信息" :visible.sync="visible">
        <el-form :model="addTeacherForm" label-position="left" label-width="80px" :rules="addTeacherRules">
          <el-form-item label="教师工号" prop="uid">
            <el-input v-model="addTeacherForm.uid" autocomplete="off"></el-input>
          </el-form-item>
            <el-form-item label="教师姓名" prop="name">
              <el-input v-model="addTeacherForm.name" autocomplete="off"></el-input>
            </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addTeacherForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="职称" prop="title">
            <el-input v-model="addTeacherForm.title" autocomplete="off"></el-input>
          </el-form-item>
              <el-form-item label="类型" prop="type">
                <el-select
                    class="term-select"
                    @change="handleTypeChange"
                    v-model="addTeacherForm.type"
                    placeholder="请选择教师类型"
                >
                  <el-option v-for="item in typeData"
                             :key="item.value"
                             :label="item.label"
                             :value="item.value">
                  </el-option>
                </el-select>
          </el-form-item>
          <el-form-item label="所属学院" prop="college">
            <el-select
                class="term-select"
                @change="handleCollegesChange"
                v-model="addTeacherForm.college_id"
                placeholder="请选择所属学院"
            >
              <el-option v-for="item in collegeData"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="addTeacher()">提 交</el-button>
        </div>
      </el-dialog>

      <!--编辑教师信息表单-->
      <el-dialog title="编辑教师信息" :visible.sync="editVisible">
        <el-form :model="editTeacherForm" label-position="left" label-width="80px" :rules="addTeacherRules">
          <el-form-item label="教师姓名" prop="name">
            <el-input v-model="editTeacherForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="editTeacherForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="职称" prop="title">
            <el-input v-model="editTeacherForm.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select
                class="term-select"
                @change="handleTypeChange"
                v-model="editTeacherForm.type"
                placeholder="请选择教师类型"
            >
              <el-option v-for="item in typeData"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属学院" prop="college">
            <el-select
                class="term-select"
                @change="handleCollegesChange"
                v-model="editTeacherForm.college_id"
                placeholder="请选择所属学院"
            >
              <el-option v-for="item in collegeData"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateTeacher()">提 交</el-button>
        </div>
      </el-dialog>

      <!-- 上一页，当前页，下一页 -->
      <div class="footer-button">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="page"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total"
            background
        ></el-pagination>
      </div>

    </template>
  </el-main>
</div>

</template>

<script>
import axios from "axios";

export default {
  name: "TeacherSelect",
  data() {
    return {
      page:1,
      pageSize:10,
      total:0,
      inputTeacherById:'',
      inputTeacherByName:'',
      tableData:[],
      typeData: [
        {
          label: '教务',
          value: 1
        },
        {
          label: '讲师',
          value: 2
        }
      ],
      collegeData: [
        {
          label: '信息学院',
          value: 1
        },
        {
          label: '经管学院',
          value: 2
        }
      ],

      // 是否支持dialog
      visible: false,
      editVisible: false,
      // dialog表单内数据
      addTeacherForm: {
        uid: '',
        name: '',
        password:'',
        title: '',
        type: '',
        college_id: '',
      },
      editTeacherForm:{
        uid: '',
        name: '',
        password:'',
        title: '',
        type: '',
        college_id: '',
      },

      // dialog表单验证规则
      addTeacherRules: {
        uid: [{required: true, message: '请输入教师工号', trigger: 'blur'}],
        name: [{required: true, message: '请输入教师姓名', trigger: 'blur'}],
        password:  [{required: true, message: '请输入教师密码', trigger: 'blur'}],
        title: [{required: true, message: '请输入职称', trigger: 'blur'}],
        type: [{required: true, message: '请输入教师类型', trigger: 'blur'}],
        college_id: [{required: true, message: '请输入所属学院', trigger: 'blur'}]
      }
    }
  },
  mounted() {
    this.showAllTeachers(this.currentPage,this.pageSize);
    this.getTotalTeacher();
  },
  methods: {
    //删除行
    deleteRow(index, row) {
      // 创造本行对象
      let target = Object.assign({}, row);
      this.$axios.defaults.headers.common['token'] = window.localStorage.getItem('token');
      this.$axios(
          {
            method: "get",
            url: 'http://localhost:8085/teacher/teacherManagement/deleteTeacher/' + target.uid,
          }
      ).then(res => {
        if (res.data.code === 200) {
          this.getTotalTeacher();
          this.showAllTeachers(this.currentPage,this.pageSize);
          this.$message({message: "删除教师信息成功！", type: "success"})
          // 前端删除本行的动画
          row.splice(index, 1);
        }
      })
    },

// 编辑行
    editRow(index, row) {
      this.editVisible = true;
      this.editTeacherForm = Object.assign({}, row);
    },
    //通过ID获取对应教师信息
    searchTeacherById() {
      this.$axios.defaults.headers.common['token'] = window.localStorage.getItem('token');
      this.$axios(
          {
            method:"post",
            url:'http://localhost:8085/teacher/teacherManagement/getTeacherById',
            data:{
              uid:this.inputTeacherById,
              total:1,
            }
          }
      ).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data;
        }
      })
    },
    //通过姓名获取对应教师信息
    searchTeacherByName(){
      this.$axios.defaults.headers.common['token'] = window.localStorage.getItem('token');
      this.$axios(
          {
            method:"post",
            url:'http://localhost:8085/teacher/teacherManagement/getTeacherByName',
            data:{
              name:this.inputTeacherByName
            }
          }
      ).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data;
        }
      })
    },
    //获取教师记录条数
    getTotalTeacher() {
      this.$axios.defaults.headers.common['token'] = window.localStorage.getItem('token');
      this.$axios(
          {
            method: "get",
            url: 'http://localhost:8085/teacher/teacherManagement/getTotalTeacher',
          }
      ).then(res => {
        if (res.data.code === 200) {
          this.total = res.data.data;
        }
      })
    },
    //获取所有教师的信息
    showAllTeachers(currentPage, pageSize) {
      this.$axios.defaults.headers.common['token'] = window.localStorage.getItem('token');
      // 异步请求显示所有数据
      currentPage = 1;
      pageSize = 10;
      this.$axios(
          {
            method:"get",
            url:"http://localhost:8085/teacher/teacherManagement/showAllTeachers/" + currentPage + "/" + pageSize,
          }
      ).then((res) => {
            console.log(res);
            this.tableData = res.data.data;

          })
    },
    // 更新教师信息
    updateTeacher(){
      this.$axios.defaults.headers.common['token'] = window.localStorage.getItem('token')
      this.$axios(
          {
            method: "post",
            url: "http://localhost:8085/teacher/teacherManagement/updateTeacher",
            data: this.editTeacherForm
          }
      )
          .then(res => {
            if (res.data.code === 200) {
              // 添加完成
              this.editVisible = false;
              this.showAllTeachers(this.currentPage,this.pageSize);
              this.$message({message: "更新教师信息成功！", type: "success"})
            } else {
              alert(res.data.message)
            }
          })
          .catch(error => {

          })
    },
    // 手动添加教师信息
    addTeacher(){
      this.$axios.defaults.headers.common['token'] = window.localStorage.getItem('token')
      this.$axios(
          {
            method: "post",
            url: "http://localhost:8085/teacher/teacherManagement/addTeacher",
            data: this.addTeacherForm
          }
      )
          .then(res => {
            if (res.data.code === 200) {
              // 添加完成
              this.visible = false;
              this.getTotalTeacher();
              this.showAllTeachers(this.currentPage,this.pageSize);
              this.$message({message: "添加教师信息成功！", type: "success"})
            } else {
              alert(res.data.message)
            }
          })
          .catch(error => {

          })
    },
    //教师身份信息转换
    typeFormat(row, column, cellValue, index) {
     switch (cellValue) {
       case 1:
         return '教务';
       case 2:
         return '讲师';
    }
   },
    //所属学院信息转换
    collegeFormat(row, column, cellValue, index) {
      switch (cellValue) {
        case 1:
          return '信息学院';
        case 2:
          return '经管学院';
      }
    },

    handleSizeChange() {
    },
    handleCurrentChange() {
    },
    //类型绑定值
    handleTypeChange(val) {
      this.type = val;
    },
    //学院绑定值
    handleCollegesChange(val) {
      this.college_id = val;
    },
    // 手动添加教师信息按钮响应
    addTeacherInfo() {
      this.visible = true;
    },




  },
}
</script>

<style scoped>

</style>